﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 模糊化UI脚本，用于模糊到清晰的功能
    /// </summary>
    [DisallowMultipleComponent]
    [AddComponentMenu("Nirvana/UI/Effects/UI Blur")]
    [ExecuteInEditMode]
    [RequireComponent(typeof(UIMaterialEffect))]
    public sealed class UIBlur : MonoBehaviour
    {
        [SerializeField]
        [Range(0f, 0.25f)]
        private float blur;

        private UIMaterialEffect uimateffect;
        
        public float Blur
        {
            get
            {
                return this.blur;
            }
            set
            {
                if(this.blur != value)
                {
                    this.blur = value;
                    this.Refresh();
                }
            }
        }


        void Awake()
        {
            this.Refresh();
        }
#if UNITY_EDITOR
        void OnValidate()
        {
            this.Refresh();
        }
#endif
        private void Refresh()
        {
#if UNITY_EDITOR
            PrefabType prefabtype = PrefabUtility.GetPrefabType(this.gameObject);
            if (prefabtype == PrefabType.Prefab) return;

            if (this.uimateffect == null) this.uimateffect = this.GetOrAddComponent<UIMaterialEffect>();
            MaterialsEffect materialsEffect = this.uimateffect.MaterialsEffect;
            materialsEffect.blurDistance = blur;
            this.uimateffect.MaterialsEffect = materialsEffect;

            this.uimateffect.Save();
#endif
        }
    }
}


